<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html>
 <head>
    <title>生产派工管理</title>
    <!-- import css and js -->
	<%@ include file="/common/meta.jsp" %>
	<script src="${ctx}/js/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/jsp/xxkj/logic/prodispat.js"></script>
	<script type="text/javascript">
		var basic_Url="${ctx}/logic/prodispat";
		var basic_WUrl="logic/prodispat";
		var basic_Title="生产派工";
		var show_Width=380;
		var show_Hight=400;
		function createSearch(){
			init(1,8,'100,100,100,100,100,100,100,100','10%');
			//第一行的数据
			addCommon(0,0,"lable","PI编号：");
			addCommon(0,1,"txtlike","zd003PiCode");
			addCommon(0,2,"lable","生产派工号：");
			addCommon(0,3,"txtlike","zd001FormCode");
			addCommon(0,4,"lable","单据状态：");
			addCommon(0,5,"selectEnum","zd008Billstatus","<option></option><option value='PRODISPAT_ZSAVE'>待派工</option>"+
			"<option value='PRODISPAT_ZAUDIT'>部分派工</option><option value='PRODISPAT_ZAPPROVE'>派工完成</option>"+
			"<option value='PRODISPAT_ZEND'>完结</option>");
		}
		
		function searchAP(){
			multipleSearch("#table1",true,"",basic_Url+"/list.do");
		}
		document.onkeydown=keyListener;   
		function keyListener(e){   
		    e = e ? e : event;  
		    if(e.keyCode == 13){   
		    	searchAP();
		    }   
		}  
		/*
		 * 删除订单
		 */
		function checkdel(){
			var manager = $("#table1").ligerGetGridManager(); 
			var rows = manager.getSelectedRows();
			if (rows==null || 0== rows.length){
				$.dialog.alert('请选择需要操作删除的行！');
				return;
			}
			var canDel = true;
			$(rows).each(function() {
				if (!(this.zd008Billstatus=="PRODISPAT_SAVE" )){
					canDel = false;
					$.dialog.alert('您删除的单据中,存在不能删除操作的单据状态！');
				}
				
				if(this.bizCreatorName !='${user.name}'){
					canDel = false;
					$.dialog.alert('您删除的单据中,存在非本人的单据，不能删除操作!');
				}
			});
			
			if(canDel){
				del();//ajax 删除
			}
		}
		//派工
		function dispatchTo(){
			var manager = $("#table1").ligerGetGridManager(); 
			var cRows = manager.getSelectedRows();
			if (cRows==null || 0== cRows.length){
				$.dialog.alert('请选择需要派工的行！');
				return;
			}else{
				if(cRows.length>1){
					$.dialog.alert('每次只能单行派工！');
					return ;
				}
				var dipatchingId = cRows[0].id;
				var url = basic_WUrl+"/operate/edit/"+dipatchingId+".do";
				ShowIframeDialog("operate","派工",url,show_Width,show_Hight);
			}
		}
		
		//完结该生产派工
		function dispatchEnd(){
			var manager = $("#table1").ligerGetGridManager(); 
			var cRows = manager.getSelectedRows();
			if(cRows==null || cRows.length==0){
				$.dialog.alert('请选择需要完结的派工！');
				return;
			}
			var canSm = true;
			for (var i = 0;i<cRows.length;i++){
				var row= cRows[i];
				if(row.zd008Billstatus=='PRODISPAT_ZAPPROVE'){
					$.dialog.alert("第" + (i+1) + "行的派工单已派工完成，不能完结。");
					canSm = false;
					return false;
				}
			}
			if(canSm){
				var idStr = "";
				for(var i=0; i<cRows.length; i++){
					if(i!=0){
						idStr+=",";
				 	}
					//获得行数据
				 	idStr += cRows[i].id;		
				}
				getDG().dialog({content: "请确定选择完结数据(Yes或者No)!",width: 250,lock: true,max: false,min: false,
					title:'Confirm Infor',
					button: [{name: 'Yes',
					          	callback: function(){
						            $.ajax({
						            	url: basic_Url+"/patchingEnd.do",
					           	      	global: false,
					           	      	type: "POST",
					           	      	dataType: "json",
					           	      	data: ({idStr : idStr}),		     
					           	      	success: function(msg){
					           	    		manager.removeRange(cRows);
					           	    	  	$.dialog.alert(msg.infor);  		    	
					           	      	}
						           	});
					            }
					         },
					         {
					        	   name: 'No',
						           focus: true
					         }             
					     ]
				}).zindex();
			}
		}
	</script>
	
  </head>
 <body style="padding:10px" onload="createSearch()">  
       <div id="layout1">
          <div class="panelBar">
	            <ul class="toolBar">
		           <li><a class="add" onclick="dispatchTo()"><span>派工</span></a></li>
		           <li><a class="add" onclick="dispatchEnd()"><span>完结</span></a></li>
				   <li><a class="delete" onclick="checkdel()"><span>删除</span></a></li>
               </ul>
          </div>
          <div id="pageHeader" class="pageHeader">
          <input type="hidden" id="type" name="type" value="${type}"/>
               <div class="searchBar" id="multipleSearchDialog">
	                <table class="table" width="100%" id="searchHeader" >  
                        <tbody id="newbody">
                        </tbody>
 	                </table>
                    <div class="subBar">
			             <ul>
				             <li><div class="buttonActive"><div class="buttonContent"><button type="submit" id="btnSearch" onclick="searchAP()">查询</button></div></div></li>
				             <li><a class="button" onclick="clearSearch()"><span>重置</span></a></li>
			             </ul>
	                </div>
               </div>
          </div>
      </div>
      <div id="table1">
      </div>
      <div class="clear"></div>
    </body>
</html>